﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace WindowsFormsApp_h1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        async private void button1_Click(object sender, EventArgs e)
        {
            string address = "https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + textBox1.Text;
            //string content = await AccessTheWebAsync(Parse(address));
            string content = await AccessTheWebAsync(address);
            string content1 = await AccessTheWebAsync(Parse(content));
           //textBox2.Text = StripHTML( content,1000);
            textBox2.Text = getProContent(content1, 200);
        }
        async Task<string> AccessTheWebAsync(string url)
        {
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync(url);
            DoIndependentWork();
            string urlContents = await getStringTask;
            return urlContents;
        }
        void DoIndependentWork()
        {
            textBox2.Text += "Working......\r\n";
        }
        public string Parse(string html)
        {
            string strRef = @"(href|HREF)[]*=[]*[""'][^""'#>]+[""']";
            string check = @"[\w]*\.html";
            MatchCollection matches = new Regex(strRef).Matches(html);
            Regex regex = new Regex(check);
            string res = " ";
            foreach (Match match in matches)
            {
                strRef = match.Value.Substring(match.Value.IndexOf('=') + 1).Trim('"', '\"', '#', ' ', '>');
                if (strRef.Length == 0) continue;
                if (regex.IsMatch(strRef))
                {
                    return reset(strRef,html);
                }
            }
            return res;
        }
        public string reset(string strRef, string url)//绝对地址
        {
            if (strRef.StartsWith("http")) return strRef;

            else if (strRef.StartsWith("//"))
                return "https:" + strRef;
            else if (strRef.StartsWith("/"))
            {
                int t = url.IndexOf(".com");
                string a = url.Substring(0, t + 4);
                return a + strRef;
            }
            else if (strRef.StartsWith("./"))
            {
                if (url.EndsWith("/"))
                {
                    return url + strRef.Substring(2);
                }
                else return url + strRef.Substring(1);
            }
            else if (strRef.StartsWith("../"))
            {
                int n = url.Length;
                string s = url;
                if (s.EndsWith("/"))
                {
                    s = url.Substring(0, n - 1);
                }
                int t = s.LastIndexOf("/");
                s = s.Substring(0, t);
                return url + strRef.Substring(2);
            }
            return url;
        }
        public string StripHTML(string strHtml,int num)
        {
            string[] aryReg ={
          @"<script[^>]*?>.*?</script>",

          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
          @"([\r\n])[\s]+",
          @"&(quot|#34);",
          @"&(amp|#38);",
          @"&(lt|#60);",
          @"&(gt|#62);",
          @"&(nbsp|#160);",
          @"&(iexcl|#161);",
          @"&(cent|#162);",
          @"&(pound|#163);",
          @"&(copy|#169);",
          @"&#(\d+);",
          @"-->",
          @"<!--.*\n"

         };

            string[] aryRep = {
           "",
           "",
           "",
           "\"",
           "&",
           "<",
           ">",
           " ",
           "\xa1",//chr(161),
           "\xa2",//chr(162),
           "\xa3",//chr(163),
           "\xa9",//chr(169),
           "",
           "\r\n",
           ""
          };

            string newReg = aryReg[0];
            string strOutput = strHtml;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }

            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            strOutput = Regex.Replace(strOutput, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
            strOutput = strOutput.Substring(0, num);
            return strOutput;
        }
        public string getProContent(string content, int num)
        {

            string result = "";
            if (string.IsNullOrEmpty(content))
                return "";
            //去除\r\n\t
            result = content.Replace("\r", " ");
            result = result.Replace("\n", " ");
            result = result.Replace("\t", " ");

            //去除<>以内的内容
            result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);

            //去掉特殊转义字符
            result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase);
            result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);

            //去除多余的空行空格
            result = Regex.Replace(result, @" ( )+", " ");
            result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
            result = Regex.Replace(result, @"(\r\r)+", "\r\n");

            if (result.Length < num)
            {
                return result;
            }
            return result.Substring(0, num) + "...";
        }
    }

}
